package cn.yunyichina.provider.base.service.impl;

import cn.yunyichina.provider.base.dao.ApiClassDao;
import cn.yunyichina.provider.base.dao.RuleQueryDao;
import cn.yunyichina.provider.base.entity.ApiClass;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yunyichina.provider.iface.entity.common.ResponseObject;

import java.util.Date;
import java.util.List;

/**
 * 接口分类
 */
@Service("apiclassServiceImpl")
public class ApiclassServiceImpl {

    @Autowired
    private ApiClassDao apiClassDao;

    public ResponseObject getApiclass(String data) {
        ApiClass wheres = JsonUtils.parseObject(data, ApiClass.class);

        ResponseObject response = new ResponseObject();
        response.setResultCodeAndMessage("0", "调用成功");
        List<ApiClass> list = apiClassDao.select(wheres);
        response.setResultObject(list);
        return response;
    }

    public ResponseObject listApiclass( ) {
        ResponseObject response = new ResponseObject();
        response.setResultCodeAndMessage("0", "调用成功");
        List<ApiClass> list = apiClassDao.select();
        response.setResultObject(list);
        return response;
    }

    public ResponseObject AddApiclass(String data) {
        ApiClass apiClass = JsonUtils.parseObject(data, ApiClass.class);
        apiClass.setId(PKGenerator.generateId());
        apiClass.setCreatedTime(DateUtils.dateToString(new Date()));
        ResponseObject response = new ResponseObject();
        response.setResultCodeAndMessage("0", "保存成功");
       apiClassDao.insert(apiClass);
        return response;
    }

    public ResponseObject updateApiclass(String data) {
        ApiClass apiClass = JsonUtils.parseObject(data, ApiClass.class);
        ResponseObject response = new ResponseObject();
        if(StringUtils.isBlank(apiClass.getId())){
            response.setResultCodeAndMessage("1001", "更新id参数不能为空");
            return response;
        }
        response.setResultCodeAndMessage("0", "更新成功");
        apiClassDao.update(apiClass);
        return response;
    }

    public ResponseObject deleteApiclass(String data) {
        ApiClass apiClass = JsonUtils.parseObject(data, ApiClass.class);
        ResponseObject response = new ResponseObject();
        if(StringUtils.isBlank(apiClass.getId())){
            response.setResultCodeAndMessage("1001", "删除id参数不能为空");
            return response;
        }
        response.setResultCodeAndMessage("0", "删除成功");
        apiClassDao.delete(apiClass);
        return response;
    }
}
